<template>
  <div class="page-wrapper">
    <PageTools>
      <template #after>
        <el-button type="primary" size="small" @click="showAddDialog(1, '0')">新增</el-button>
      </template>
    </PageTools>
    <el-card>
      <el-table border :data="list" row-key="id">
        <el-table-column label="名称" prop="name" />
        <el-table-column label="标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button type="text" @click="showAddDialog(2, scope.row.id)">新增</el-button>
            <el-button type="text" @click="showEditDialog(scope.row.id)">编辑</el-button>
            <el-button type="text" @click="delPermisstion(scope.row.id)">删除</el-button>
          </template>
        </el-table-column></el-table>
    </el-card>
    <el-dialog :visible="isShowDialog" :title="formData.id?'编辑权限':'新增权限'" @close="btnCancel">
      <el-form ref="addForm" :model="formData">
        <el-form-item label="权限名称">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button size="small" type="primary" @click="btnOk">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getPermList, getPermissionDetail, updatePermission, delPermission, addPermission } from '@/api/permission'
import { listToTree } from '@/utils'
export default {
  data() {
    return {
      list: [],
      isShowDialog: false,
      formData: {
        name: '',
        code: '',
        description: ''
      }
    }
  },
  created() {
    this.loadList()
  },
  methods: {
    async loadList() {
      const res = await getPermList()
      console.log(res)
      this.list = listToTree(res, '0')
      console.log(this.list)
    },
    async showEditDialog(id) {
      this.formData = await getPermissionDetail(id)
      console.log(this.formData)
      this.isShowDialog = true
    },
    showAddDialog(type, pid) {
      this.formData.type = type
      this.formData.pid = pid
      this.isShowDialog = true
    },
    async btnOk() {
      await this.$refs.addForm.validate()
      // 通过表单 formData 是否拥有 id 决定是新建还是编辑
      if (this.formData.id) {
        // 编辑
        await updatePermission(this.formData)
      } else {
        // 新增
        await addPermission(this.formData)
      }
      // 提示用户
      this.$message.success('操作成功')
      // 关闭弹窗
      this.isShowDialog = false
      // 更新页面
      this.loadList()
    },

    btnCancel() {
      this.formData = {
        name: '',
        code: '',
        description: ''
      }
      this.isShowDialog = false
    },
    // 删除
    async delPermisstion(id) {
      await this.$confirm('是否确认删除?')
      await delPermission(id)
      this.$message.success('删除成功')
      this.loadList()
    }

  }
}
</script>

<style>

</style>
